﻿define(["jquery", "jquery.fancybox"], function ($, fancybox) {
    var alert = function (options) {
        var defaults = {
            title: '提示',
            content: '',
            contentId: null,
            sureText: '确定',
            beforeOpen: null,
            afterOpen: null,
            afterClose: null,
            sure: function () { $.fancybox.close(); }
        };
        var opts = $.extend(defaults, options);
        if ($("#boxAlert").length >= 1) {
            $.fancybox.close();
        }
        var box = $("<div class='box' id='boxAlert' style='min-width:300px;'><div class='box-title'></div><div class='box-content text-center' style='padding:20px;'></div>" +
            "<div class='box-footer text-center' style='white-space:nowrap;'>" +
            "<button type='button' class='btn btn-success' id='btnBoxSure'>" + opts.sureText + "</button>" +
            "</div>" +
            "</div>");
        var bModal = false;
        box.children(".box-title").text(opts.title);
        box.children(".box-title").append("<a href='javascript:$.fancybox.close();' class='pull-right' style='color:#fff;'><i class='fa fa-times'></i></a>");
        if (opts.contentId) {
            bModal = false;
            box.children(".box-content").html($(opts.contentId).html());
        } else {
            bModal = true;
            box.children(".box-content").html(opts.content);
        }
        $("body").append(box);
        box.find("#btnBoxSure").click(opts.sure);
        box.find("#btnBoxCancel").click(opts.cancel);
        $.fancybox.open("#boxAlert", {
            padding: 0,
            fitToView: false,
            autoSzie: false,
            closeBtn: false,
            modal: bModal,
            scrolling: 'no',
            beforeShow: opts.beforeOpen,
            afterShow: opts.afterOpen,
            afterClose: function () {
                $("#boxAlert").remove();
                if (opts.afterClose) {
                    opts.afterClose();
                }
            }
        });
        $("#btnBoxSure").focus();
    }

    var confirm = function (options) {
        var defaults = {
            title: '确认框',
            content: '',
            contentId: null,
            sureText: '确定',
            cancelText: '取消',
            beforeOpen: null,
            afterOpen: null,
            sure: function () { },
            cancel: function () { $.fancybox.close(); }
        };
        var opts = $.extend(defaults, options);

        if ($("#boxAlert").length >= 1) {
            $.fancybox.close();
        }

        var box = $("<div class='box' id='boxAlert' style='min-width:300px;'><div class='box-title'></div><div class='box-content text-center' style='padding:20px;'></div>" +
            "<div class='box-footer text-center' style='white-space:nowrap;'>" +
            "<button type='button' class='btn btn-success' id='btnBoxSure'>" + opts.sureText + "</button>&nbsp;&nbsp;&nbsp;&nbsp;<button type='button' class='btn btn-success' id='btnBoxCancel'>" + opts.cancelText + "</button>" +
            "</div>" +
            "</div>");

        box.children(".box-title").text(opts.title);
        box.children(".box-title").append("<a href='javascript:$.fancybox.close();' class='pull-right' style='color:#fff;'><i class='fa fa-times'></i></a>");
        if (opts.contentId) {
            box.children(".box-content").html($(opts.contentId).html());
        } else {
            box.children(".box-content").html(opts.content);
        }
        $("body").append(box);
        box.find("#btnBoxSure").click(opts.sure);
        box.find("#btnBoxCancel").click(opts.cancel);
        $.fancybox.open("#boxAlert", {
            padding: 0,
            fitToView: false,
            autoSzie: false,
            closeBtn: false,
            modal: false,
            scrolling: 'no',
            beforeShow: opts.beforeOpen,
            afterShow: opts.afterOpen,
            afterClose: function () { $("#boxAlert").remove(); }
        });
        $("#btnBoxCancel").focus();
    }

    var show = function (options) {
        var defaults = {
            title: '提示',
            contentId: null,
            beforeOpen: null,
            afterOpen: null,
            afterClose: null
        };
        var opts = $.extend(defaults, options);
        if ($("#boxAlert").length >= 1) {
            $.fancybox.close();
        }
        var box = $("<div class='box' id='boxAlert' style='min-width:300px;'><div class='box-title'></div><div class='box-content text-center' style='padding:20px;'></div>" +
            "<div class='box-footer text-center' style='white-space:nowrap;'>" +
            "</div>" +
            "</div>");

        box.children(".box-title").text(opts.title);
        box.children(".box-title").append("<a href='javascript:$.fancybox.close();' class='pull-right' style='color:#fff;'><i class='fa fa-times'></i></a>");
        if (opts.contentId) {
            box.children(".box-content").html($(opts.contentId).html());
        } else {
            return;
        }
        $("body").append(box);
        $.fancybox.open("#boxAlert", {
            padding: 0,
            fitToView: false,
            autoSzie: false,
            closeBtn: false,
            scrolling: 'no',
            beforeShow: opts.beforeOpen,
            afterShow: opts.afterOpen,
            modal: true,
            afterClose: function () {
                $("#boxAlert").remove();
                if (options.afterClose) {
                    options.afterClose();
                }
            }
        });
    }
    var close = function () {
        $.fancybox.close();
    }
    return {
        alert: alert,
        close: close,
        confirm: confirm,
        show: show
    };
});